<?php
module_load_include('inc', 'lastbuyer', 'lastbuyer');
module_load_include('inc', 'crf', 'crf');
module_load_include('inc', 'lastbuyer', 'lastbuyer.api');
function update_transactions(){
  $output = drupal_get_form('transaction_filter_form');
  $output .= list_transations();
  return $output;
}
function list_transations(){
  $session = &$_SESSION['lastbyer_transaction_filter'];
  $session = (is_array($session) && ! empty($session)) ? $session : array('status'=>0,'in_day'=>-1);
  $where = " ";
  $first = true;
  $param_arr = array();
  foreach ($session as $key => $value) {
    if($value != NULL && $value != -1){
      if($first){
        $first =  false;
        $where .=" WHERE t." .$key.'=%d ';
      }else{
        $where .= ' AND t.'.$key.'=%d ';
      }
      $param_arr[]= $value;
    }
  }
  $sql  = "SELECT t.tid,t.uid,t.status,t.nid,t.point,t.idx,t.in_day,t.buy_date,t.trace_id,n.title,u.name,t.ref_date FROM {lastbuyer_transaction} AS t "
  ." JOIN {users} AS u ON t.uid = u.uid JOIN {node} AS n ON n.nid=t.nid "
  .$where;
  $pageSize =10;
  array_unshift($param_arr,$sql,$pageSize,get_pager_index('list_transaction_all_admin'),NULL);
  $query = call_user_func_array("pager_query", $param_arr);
  $array_status =_get_status_array();
  $array_in_day =  _get_in_day_status_array();
  while ($r = db_fetch_array($query)) {
    $data =  array();
    $data[] = l($r['name'],'user/'.$r['uid']);
    $data[] = l($r['title'],'node/'.$r['nid']);
    $data[] = $array_status[$r['status']];
    $data[] = $array_in_day[$r['in_day']];
    $data[] = date('D, d M Y H:i:s',$r['buy_date']);
    $data[] = $r['idx'];
    $data[] = $r['trace_id'];
    $data[] = date('D, d M Y H:i:s',$r['ref_date']);
    $rows[] = $data;
  }
  $header = array(t('user id'),t('product id'),t('status'),t('in day'),t('buy date'),t('index'),t('trace id'),t('Reference date'));
  return theme('table', $header, $rows).theme('pager', array('«','‹','|','›','»'), $pageSize,get_pager_index('list_transaction_all_admin'),array(),3);
}
function add_sponsor_form(&$form_state){
  $form['name'] =array(
      '#type' => 'textfield', 
      '#title' => t('Name'), 
      '#default_value' => '', 
  );

  $form['image']= array(
      '#type' => 'textfield', 
      '#title' => t('Image'), 
      '#default_value' => '', 
  );
  $form['link']= array(
      '#type' => 'textfield', 
      '#title' => t('Link'), 
      '#default_value' => '',
  );
  $form['weight']= array(
      '#type' => 'textfield', 
      '#title' => t('Weight'), 
      '#default_value' => '0',
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Add'));
  return $form;
}
function add_sponsor_form_submit($form, &$form_state) {
  $name =$form_state['values']['name'];
  $image =$form_state['values']['image'];
  $link =$form_state['values']['link'];
  $weight =$form_state['values']['weight'];
  $sql =  "INSERT INTO {sponsor} (name,image,link,weight) VALUES ('%s','%s','%s',%d)";
  db_query($sql,$name,$image,$link,$weight);
}
function list_sponsor(){
  $header = array(t('name'),t('logo'),t('edit'),t('delete'));
  $sql = 'SELECT * FROM {sponsor} ORDER BY weight ASC';
  $pageSize = 5;
  $query = pager_query($sql,$pageSize,get_pager_index('sponsor_list_pager'),NULL);
  while ($r = db_fetch_array($query)) {
    $data =  array();
    $data[] = $r['name'];
    $data[] = theme('image',$r['image'],'','',array('height' => 64),FALSE);
    $data[] = l('edit','last_buyer/settings/sponsor/edit/'.$r['sid']);
    $data[] = l('delete','last_buyer/settings/sponsor/delete/'.$r['sid']);
    $rows[] = $data;
  }
  $result = theme('table', $header, $rows);
  $result .=theme('pager', array('«','‹','|','›','»'), $pageSize,get_pager_index('sponsor_list_pager'));
  return $result;
}
function edit_sponsor_form(&$form_state,$sid){
  $form['sid']=  array('#type' => 'hidden', '#value' => $sid);
  $sql = 'SELECT * FROM {sponsor} WHERE sid=%d';
  $query = db_query($sql,$sid);
  $r = db_fetch_array($query);
  $form['name'] =array(
      '#type' => 'textfield', 
      '#title' => t('Name'), 
      '#default_value' => isset($r['name'])?$r['name']:'', 
  );

  $form['image']= array(
      '#type' => 'textfield', 
      '#title' => t('Image'), 
      '#default_value' => isset($r['image'])?$r['image']:'', 
  );
  $form['link']= array(
      '#type' => 'textfield', 
      '#title' => t('Link'), 
      '#default_value' => isset($r['link'])?$r['link']:'', 
  );
  $form['weight']= array(
      '#type' => 'textfield', 
      '#title' => t('Weight'), 
      '#default_value' =>isset($r['weight'])?$r['weight']:'0', 
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return $form;
}
function edit_sponsor_form_submit($form, &$form_state) {
  $sid =$form_state['values']['sid'];
  $name =$form_state['values']['name'];
  $image =$form_state['values']['image'];
  $link =$form_state['values']['link'];
  $weight =$form_state['values']['weight'];
  $sql =  "UPDATE {sponsor}  SET name='%s',image='%s',link='%s',weight=%d WHERE sid=%d";
  if(db_query($sql,$name,$image,$link,$weight,$sid) != FALSE){
    drupal_set_message(t('Update success!'));
  }else{
    drupal_set_message(t('Update failt'));
  }
}
function delete_sponsor($sid){
  $sql =  "DELETE FROM {sponsor}  WHERE sid=%d";
  $primary_sponsor =variable_get('sponsor_block_primary_sponsor',0);
  if(db_query($sql,$sid) != FALSE){
    drupal_set_message(t('Delete success!'));
  }else{
    drupal_set_message(t('Delete failt'));
  }
  if($sid == $primary_sponsor){
    variable_del('sponsor_block_primary_sponsor');
  }
  drupal_goto('last_buyer/settings/sponsor');
}

function transaction_filter_form(&$form_state){
  $session = &$_SESSION['lastbyer_transaction_filter'];
  $session = (is_array($session) && ! empty($session))? $session : array('status'=>0,'in_day'=>-1);

  $form['nid'] =array(
      '#type' => 'textfield', 
      '#title' => t('Product id'), 
      '#default_value' => $session['nid'], 
  );

  $form['status']= array(
      '#type' => 'select', 
      '#title' => t('Status'), 
      '#default_value' => $session['status'],
      '#options' => _get_status_array(),
  );
  $form['in_day']= array(
      '#type' => 'select', 
      '#title' => t('In day status'), 
      '#default_value' => $session['in_day'],
      '#options' =>_get_in_day_status_array(),
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Filter'));
  return $form;
}
function transaction_filter_form_submit($form, &$form_state) {
  $nid =$form_state['values']['nid'];
  $status =$form_state['values']['status'];
  $in_day =$form_state['values']['in_day'];
  $_SESSION['lastbyer_transaction_filter'] = array('nid'=>$nid,'status'=>$status,'in_day'=>$in_day);
}

?>